package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.CategoryDisableFailedException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 分类分页查询接口
     */
    @Override
    public PageResult getCategoryByPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> p = categoryMapper.getCategoryByPage(categoryPageQueryDTO.getName(),categoryPageQueryDTO.getType());
        PageResult pr = new PageResult();
        pr.setTotal(p.getTotal());
        pr.setRecords(p.getResult());
        return pr;
    }

    /**
     * 分类新增接口
     */
    @Override
    public void insertCategory(Category category) {
        Long empId = BaseContext.getCurrentId();
        /*category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(empId);
        category.setUpdateUser(empId);*/
        category.setStatus(StatusConstant.DISABLE);
        categoryMapper.insertCategory(category);
    }

    /**
     * 根据id删除分类接口
     * 需要判断当前分类下是否存在套餐或者菜品，如果存在就抛异常，不能删除
     */
    @Transactional
    @Override
    public void deleteCategoryById(Long id) {
        Category c = categoryMapper.getCategoryById(id);
        if (c.getType() == 1){
            Integer count1 = dishMapper.countByCategoryId(id);
            if (count1 != 0){
                throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
            }
        }else {
            Integer count2 = setmealMapper.countByCategoryId(id);
            if (count2 != 0){
                throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
            }
        }
        //没有就直接删除
        categoryMapper.deleteCategoryById(id);
    }

    /**
     * 修改分类接口
     */
    @Override
    public void updateCategory(Category category) {
        /*category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());*/
        categoryMapper.updateCategory(category);

    }

    /**
     * 启用禁用分类接口
     */
    @Transactional
    @Override
    public void updateCategoryStatus(Integer status, Long id) {
        /*Category c = Category.builder().id(id).status(status).updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId()).build();*/
        //如果是禁用分类，需要判断当前分类下是否存在启售菜品或者套餐，否则无法禁用
        if (status == 0){
            Category c = categoryMapper.getCategoryById(id);
            if (c.getType() == 1){
                Integer count1 = dishMapper.countEnableDishsInCategory(id);
                if (count1 != 0){
                    throw new CategoryDisableFailedException(MessageConstant.CATEGORY_BE_RELATED_BY_ENABLE_DISH);
                }
            }else {
                Integer count2 = setmealMapper.countEnableSetmealInCategory(id);
                if (count2 != 0){
                    throw new CategoryDisableFailedException(MessageConstant.CATEGORY_BE_RELATED_BY_ENABLE_SETMEAL);
                }
            }
        }
        Category c = Category.builder().id(id).status(status).build();
        categoryMapper.updateCategory(c);    //直接整合进修改
    }

    /**
     * 根据类型查询分类接口
     */
    @Override
    public List<Category> getCategoryByType(Integer type) {
        List<Category> list = categoryMapper.getCategoryByType(type);
        return list;
    }
}
